using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Three.BookStore.ProductionSystem.ShiftManagement;
using Three.BookStore.ProductionSystem.ShiftManagement.Dtos;

namespace Three.BookStore.Web.Controllers.ProductionSystem.ShiftManagement
{
    [Route("ProductionSystem/ShiftManagement/[controller]")]
    public class HolidaySettingController : Controller
    {
        private readonly IHolidaySettingServices _holidaySettingServices;

        public HolidaySettingController(IHolidaySettingServices holidaySettingServices)
        {
            _holidaySettingServices = holidaySettingServices;
        }

        /// <summary>
        /// 节假日设置主页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("")]
        [Route("Index")]
        public IActionResult Index()
        {
            ViewBag.Title = "节假日设置";
            // 明确指定视图路径
            return View("~/Views/ProductionSystem/ShiftManagement/HolidaySetting/Index.cshtml");
        }

        /// <summary>
        /// 获取节假日设置数据
        /// </summary>
        [HttpGet]
        [Route("GetHolidaySettings")]
        public async Task<IActionResult> GetHolidaySettings(int year, int month)
        {
            try
            {
                var startDate = new DateTime(year, month, 1);
                var endDate = startDate.AddMonths(1).AddDays(-1);
                
                System.Diagnostics.Debug.WriteLine($"查询节假日数据: {year}年{month}月 ({startDate:yyyy-MM-dd} 到 {endDate:yyyy-MM-dd})");
                
                var holidays = await _holidaySettingServices.GetHolidaySettingsAsync(startDate, endDate);
                
                System.Diagnostics.Debug.WriteLine($"找到 {holidays.Count} 条节假日数据");
                foreach (var holiday in holidays)
                {
                    System.Diagnostics.Debug.WriteLine($"  {holiday.HolidayDate:yyyy-MM-dd} = {(holiday.HolidayType == 0 ? "假" : "班")}");
                }
                
                return Json(new
                {
                    success = true,
                    data = holidays,
                    message = "获取成功"
                });
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"获取节假日数据失败: {ex.Message}");
                return Json(new
                {
                    success = false,
                    message = "获取失败：" + ex.Message
                });
            }
        }

        /// <summary>
        /// 获取指定日期的节假日设置
        /// </summary>
        [HttpGet]
        [Route("GetHolidayByDate")]
        public async Task<IActionResult> GetHolidayByDate(string date)
        {
            try
            {
                if (DateTime.TryParse(date, out DateTime targetDate))
                {
                    var holiday = await _holidaySettingServices.GetHolidaySettingByDateAsync(targetDate);
                    
                    return Json(new
                    {
                        success = true,
                        data = holiday,
                        message = "获取成功"
                    });
                }
                else
                {
                    return Json(new
                    {
                        success = false,
                        message = "日期格式错误"
                    });
                }
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    message = "获取失败：" + ex.Message
                });
            }
        }

        /// <summary>
        /// 保存节假日设置
        /// </summary>
        [HttpPost]
        [Route("SaveHolidaySetting")]
        public async Task<IActionResult> SaveHolidaySetting([FromBody] CreateOrUpdateHolidaySettingDto input)
        {
            try
            {
                var result = await _holidaySettingServices.CreateOrUpdateAsync(input);
                
                return Json(new
                {
                    success = true,
                    data = result,
                    message = "保存成功"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    message = "保存失败：" + ex.Message
                });
            }
        }

        /// <summary>
        /// 删除节假日设置
        /// </summary>
        [HttpPost]
        [Route("DeleteHolidaySetting")]
        public async Task<IActionResult> DeleteHolidaySetting(string date)
        {
            try
            {
                if (DateTime.TryParse(date, out DateTime targetDate))
                {
                    await _holidaySettingServices.DeleteByDateAsync(targetDate);
                    
                    return Json(new
                    {
                        success = true,
                        message = "删除成功"
                    });
                }
                else
                {
                    return Json(new
                    {
                        success = false,
                        message = "日期格式错误"
                    });
                }
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    message = "删除失败：" + ex.Message
                });
            }
        }

        /// <summary>
        /// 添加/修改节假日设置视图页面
        /// </summary>
        [HttpGet]
        [Route("AddOrEdit")]
        public IActionResult AddOrEdit(string date)
        {
            ViewBag.Date = date;
            return View("~/Views/ProductionSystem/ShiftManagement/HolidaySetting/AddOrEdit.cshtml");
        }
    }
} 